<?php
    @header("Content-type:text/html;charset=utf-8");
    @include_once("./connect.php");

    if(!(isset($_GET["keyWords"]) && isset($_GET["orderCol"]) && isset($_GET["orderType"]) && isset($_GET["pageIndex"]) && isset($_GET["showNum"]))){
        $obj=array();
        $obj["status"] = false;
        $obj["msg"] = "请传入完整参数";
        $json = json_encode($obj);
        exit($json);
    }

    $keyWords = $_GET["keyWords"];
    $orderCol = $_GET["orderCol"];
    $orderType = $_GET["orderType"];
    $pageIndex = $_GET["pageIndex"];
    $showNum = $_GET["showNum"];

    $searchAll = "select count(*) as count from `goodslist` where goodsName like '%$keyWords%'";
    $resultAll = mysqli_query($conn,$searchAll);
    $obj=array();

    if($resultAll){
        $item = mysqli_fetch_assoc($resultAll);
        $count = $item["count"];
        
        $maxPage = ceil($count/$showNum);
        
        if($pageIndex > $maxPage) $pageIndex = $maxPage;
        if($pageIndex < 1) $pageIndex = 1;
        
        $skidNum = ($pageIndex-1) * $showNum;

        $sql = "SELECT * FROM `goodslist` where goodsName like '%$keyWords%' order by $orderCol $orderType limit $skidNum , $showNum";

        $result = mysqli_query($conn,$sql);

        if($result){
            $list = array();
            while($item = mysqli_fetch_assoc($result)){
                $item["goodsPrice"] = $item["goodsPrice"]*1;
                array_push($list,$item);
            }
            $obj["status"] = true;
            $obj["msg"] = "成功";
            $obj["list"] = $list;
            $obj["currentPage"] = $pageIndex*1;
            $obj["maxPage"] = $maxPage*1;
            $obj["count"] = $count;
        }else{
            $obj["status"] = false;
            $obj["msg"] = "sql语句有误";
            $obj["sql"] = $sql;
        }
    }else{
        $obj["status"] = false;
        $obj["msg"] = "sql语句有误";
        $obj["sql"] = $sql;
    }

    
    echo json_encode($obj);


?>